package com.ruoyi.project.warehouse.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.warehouse.domain.RyDepotItem;
import com.ruoyi.project.warehouse.domain.RyInventoryCurrentStock;
import com.ruoyi.project.warehouse.domain.RyInventoryCurrentStockVo;
import com.ruoyi.project.warehouse.mapper.RyDepotItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import com.ruoyi.project.warehouse.mapper.WareCurrentStockMapper;
import com.ruoyi.project.warehouse.domain.WareCurrentStock;
import com.ruoyi.project.warehouse.service.IWareCurrentStockService;

/**
 * 当前库存Service业务层处理
 *
 * @author you
 * @date 2022-09-05
 */
@Service
public class WareCurrentStockServiceImpl implements IWareCurrentStockService
{
    @Autowired
    private WareCurrentStockMapper wareCurrentStockMapper;
    @Autowired
    private RyDepotItemMapper ryDepotItemMapper;


    /**
     * 查询当前库存
     *
     * @param id 当前库存主键
     * @return 当前库存
     */
    @Override
    public WareCurrentStock selectWareCurrentStockById(Long id)
    {
        return wareCurrentStockMapper.selectWareCurrentStockById(id);
    }

    /**
     * 根据仓库id查询当前仓库中的物品
     */
    public List<RyInventoryCurrentStockVo> getInventoryByDepotId(Long depotId){
        List<RyInventoryCurrentStockVo> list = wareCurrentStockMapper.getInventoryByDepotId(depotId);
        for (RyInventoryCurrentStockVo ryV : list) {
            ryV.setInventoryName(ryV.getInventoryName()+" | "+ryV.getSpecifications());
        }
        return list;
    }

    /**
     * 根据仓库id和物品id查询仓库中物品基本信息和当前库存
     * @return
     */
    public WareCurrentStock getInfoByDepotInventoryId(RyInventoryCurrentStock ryInventoryCurrentStock){
        return wareCurrentStockMapper.getInfoByDepotInventoryId(ryInventoryCurrentStock);
    }


    /**
     * 查询当前库存列表
     *
     * @param wareCurrentStock 当前库存
     * @return 当前库存
     */
    @Override
    public List<WareCurrentStock> selectWareCurrentStockList(WareCurrentStock wareCurrentStock)
    {
        List<WareCurrentStock> resList = wareCurrentStockMapper.selectWareCurrentStockList(wareCurrentStock);
        //遍历结果，查询已锁定数量
        for(WareCurrentStock wareCurrentStock1 : resList) {
            RyDepotItem ryDepotItem = new RyDepotItem();
            ryDepotItem.setDepotId(wareCurrentStock1.getDepotId());
            ryDepotItem.setInventoryId(wareCurrentStock1.getInventoryId());
            RyDepotItem resulet = ryDepotItemMapper.getLockNumById(ryDepotItem);

            //根据锁定数量计算可用数量
            if(resulet == null){//判断锁定数量是否为空
                wareCurrentStock1.setLockNumber(0.0);
                wareCurrentStock1.setUsableNum(wareCurrentStock1.getCurrentNumber() - 0);
            } else {
                wareCurrentStock1.setLockNumber(resulet.getOperNumber());
                wareCurrentStock1.setUsableNum(wareCurrentStock1.getCurrentNumber() - resulet.getOperNumber());
            }
        }
        return resList;
    }

    /**
     * 新增当前库存
     *
     * @param wareCurrentStock 当前库存
     * @return 结果
     */
    @Override
    public int insertWareCurrentStock(WareCurrentStock wareCurrentStock)
    {
        wareCurrentStock.setCreateTime(DateUtils.getNowDate());
        return wareCurrentStockMapper.insertWareCurrentStock(wareCurrentStock);
    }

    /**
     * 修改当前库存
     *
     * @param wareCurrentStock 当前库存
     * @return 结果
     */
    @Override
    public int updateWareCurrentStock(WareCurrentStock wareCurrentStock)
    {
        wareCurrentStock.setUpdateTime(DateUtils.getNowDate());
        return wareCurrentStockMapper.updateWareCurrentStock(wareCurrentStock);
    }

    /**
     * 批量删除当前库存
     *
     * @param ids 需要删除的当前库存主键
     * @return 结果
     */
    @Override
    public int deleteWareCurrentStockByIds(Long[] ids)
    {
        return wareCurrentStockMapper.deleteWareCurrentStockByIds(ids);
    }

    /**
     * 删除当前库存信息
     *
     * @param id 当前库存主键
     * @return 结果
     */
    @Override
    public int deleteWareCurrentStockById(Long id)
    {
        return wareCurrentStockMapper.deleteWareCurrentStockById(id);
    }
}
